using System;
using System.Collections;
using System.DirectoryServices;

namespace BEGDC.CC.Entity
{
	#region AdvancedSearch

	/// <summary>
	/// AdvancedSearch object for NHibernate mapped table 'AdvancedSearch'.
	/// </summary>
	[Serializable]
	public class AdvancedSearch : System.IComparable
		{
		#region Member Variables
		
		protected int _id;
		protected string _searchType;
		protected string _searchName;
		protected string _description;
		protected string _searchSQL;
		protected string _searchCondition;
		protected string _searchField;
		protected Employee _employee;
		protected static String _sortExpression = "Id";
		protected static SortDirection _sortDirection = SortDirection.Ascending;

		#endregion

		#region Constructors

		public AdvancedSearch() { }

		public AdvancedSearch( string searchType, string searchName, string description, string searchSQL, string searchCondition, string searchField, Employee employee )
		{
			this._searchType = searchType;
			this._searchName = searchName;
			this._description = description;
			this._searchSQL = searchSQL;
			this._searchCondition = searchCondition;
			this._searchField = searchField;
			this._employee = employee;
		}

		#endregion

		#region Public Properties

		public int Id
		{
			get {return _id;}
			set {_id = value;}
		}

		public string SearchType
		{
			get { return _searchType; }
			set
			{
				if ( value != null && value.Length > 10)
					throw new ArgumentOutOfRangeException("Invalid value for SearchType", value, value.ToString());
				_searchType = value;
			}
		}

		public string SearchName
		{
			get { return _searchName; }
			set
			{
				if ( value != null && value.Length > 50)
					throw new ArgumentOutOfRangeException("Invalid value for SearchName", value, value.ToString());
				_searchName = value;
			}
		}

		public string Description
		{
			get { return _description; }
			set
			{
				if ( value != null && value.Length > 100)
					throw new ArgumentOutOfRangeException("Invalid value for Description", value, value.ToString());
				_description = value;
			}
		}

		public string SearchSQL
		{
			get { return _searchSQL; }
			set
			{
				if ( value != null && value.Length > 2000)
					throw new ArgumentOutOfRangeException("Invalid value for SearchSQL", value, value.ToString());
				_searchSQL = value;
			}
		}

		public string SearchCondition
		{
			get { return _searchCondition; }
			set
			{
				if ( value != null && value.Length > 1000)
					throw new ArgumentOutOfRangeException("Invalid value for SearchCondition", value, value.ToString());
				_searchCondition = value;
			}
		}

		public string SearchField
		{
			get { return _searchField; }
			set
			{
				if ( value != null && value.Length > 1000)
					throw new ArgumentOutOfRangeException("Invalid value for SearchField", value, value.ToString());
				_searchField = value;
			}
		}

		public Employee Employee
		{
			get { return _employee; }
			set { _employee = value; }
		}

        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }
		#endregion
		
        #region IComparable Methods
        public int CompareTo(object obj)
        {
			if (!(obj is AdvancedSearch))
				throw new InvalidCastException("This object is not of type AdvancedSearch");
			
			int relativeValue;
			switch (SortExpression)
			{
				case "Id":
					relativeValue = this.Id.CompareTo(((AdvancedSearch)obj).Id);
					break;
				case "SearchType":
					relativeValue = this.SearchType.CompareTo(((AdvancedSearch)obj).SearchType);
					break;
				case "SearchName":
					relativeValue = this.SearchName.CompareTo(((AdvancedSearch)obj).SearchName);
					break;
				case "Description":
						relativeValue = ( this.Description != null ) ? this.Description.CompareTo(((AdvancedSearch)obj).Description) : -1;
					break;
				case "SearchSQL":
						relativeValue = ( this.SearchSQL != null ) ? this.SearchSQL.CompareTo(((AdvancedSearch)obj).SearchSQL) : -1;
					break;
				case "SearchCondition":
						relativeValue = ( this.SearchCondition != null ) ? this.SearchCondition.CompareTo(((AdvancedSearch)obj).SearchCondition) : -1;
					break;
				case "SearchField":
						relativeValue = ( this.SearchField != null ) ? this.SearchField.CompareTo(((AdvancedSearch)obj).SearchField) : -1;
					break;
                default:
                    goto case "Id";
			}
            if (AdvancedSearch.SortDirection == SortDirection.Ascending)
				relativeValue *= -1;
			return relativeValue;
		}
		#endregion
	}

	#endregion
}
